cc.Class({
    extends: cc.Component,

    properties: {
    },


    onLoad() {
        this.speed = 200;
        this.scaleBase = this.node.scaleX;
  


        this.xForward = 0;
        this.yForward = 0;
        this.leftKey = 37;
        this.rightKey = 39;
        this.upKey = 38;
        this.downKey = 40;
        this.canLeft = true;
        this.canRight = true;
        this.canDown = true;
        this.canUp = true;

        this.dt = 1;
        this.eatCounts=0



        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.on_key_down, this);

        this.parent = this.node.getParent();

        this.bg = {};
        this.bgNode = this.parent.getChildByName('BgNode');
        this.bgCounts = this.bgNode.childrenCount;
        for (i = 1; i <= this.bgCounts; i++) {
            this.bg[i] = this.bgNode.getChildByName('Bg' + i);
        }

        this.bean={};
        this.beanNode=this.parent.getChildByName('BeanNode');
        this.beanCounts = this.beanNode.childrenCount;
        for(i = 1; i <= this.beanCounts; i++) {
            this.bean[i] = this.beanNode.getChildByName('Bean' + i);
            this.bean[i].active=true;
        }
    },




    start() {
        this.checkEatBean = function (){
            var eaterIntersects = this.node.getBoundingBox();
            for(i=1;i<=this.beanCounts;i++)
            {
                if(this.bean[i].active)
                {
                    var bool_intersects = eaterIntersects.intersects(this.bean[i].getBoundingBox());
                    if(bool_intersects)
                    {
                        this.bean[i].active=false;
                        this.eatCounts++;
                        if(this.eatCounts>=this.beanCounts)
                        {
                            this.xForward=0;
                            this.yForward=0;
                            cc.director.loadScene('Menu');
                        }
                    }
                }
            }
        }
        this.checkCanGoOn = function (xForward, yForward, dt) {

            var canGoOn = true
            var eaterIntersects = this.node.getBoundingBox();
            eaterIntersects.x += xForward * dt * this.speed;
            eaterIntersects.y += yForward * dt * this.speed;
            eaterIntersects.width += 10;
            eaterIntersects.height += 10;
            for (i = 1; i <= this.bgCounts; i++) {
                var bool_intersects = eaterIntersects.intersects(this.bg[i].getBoundingBox());
                if (bool_intersects) {
                    canGoOn = false;

                    if (xForward > 0) { this.canRight = false; }
                    else if (xForward < 0) { this.canLeft = false; }
                    else if (yForward > 0) { this.canUp = false; }
                    else if (yForward < 0) { this.canDown = false; }
                }
                else {
                    if (xForward > 0) { this.canRight = true; }
                    else if (xForward < 0) { this.canLeft = true; }
                    else if (yForward > 0) { this.canUp = true; }
                    else if (yForward < 0) { this.canDown = true; }
                }
            }
            return canGoOn;
        }

    },



    on_key_down: function (t) {

        var xForward = 0;
        var yForward = 0;
        // console.log(t.keyCode);
        switch (t.keyCode) {

            case this.leftKey: //left
                xForward = -1;
                yForward = 0;
                if (!this.checkCanGoOn(xForward, yForward, this.dt)) {
                    return;
                }
                if (!this.canLeft) { return; }
                this.node.x -= 1;
                this.node.scaleX = -this.scaleBase;
                this.node.rotation = 0;

                break;
            case this.upKey: //up
                xForward = 0;
                yForward = 1;
                if (!this.checkCanGoOn(xForward, yForward, this.dt)) {
                    return;
                }
                if (!this.canUp) { return; }
                this.node.y += 1;
                this.node.scaleX = this.scaleBase;
                this.node.rotation = -90;

                break;
            case this.rightKey: //right
                xForward = 1;
                yForward = 0;
                if (!this.checkCanGoOn(xForward, yForward, this.dt)) {
                    return;
                }
                if (!this.canRight) { return; }
                this.node.x += 1;
                this.node.scaleX = this.scaleBase;
                this.node.rotation = 0;

                break;
            case this.downKey: //down
                xForward = 0;
                yForward = -1;
                if (!this.checkCanGoOn(xForward, yForward, this.dt)) {
                    return;
                }
                if (!this.canDown) { return; }
                this.node.y -= 1;
                this.node.scaleX = this.scaleBase;
                this.node.rotation = 90;

                break;
        }
        if (this.checkCanGoOn(xForward, yForward, this.dt)) {
            this.xForward = xForward;
            this.yForward = yForward;
        }

    },



    update(dt) {

        this.dt = dt;
        this.checkEatBean();
        if (!this.checkCanGoOn(this.xForward, this.yForward, dt)) {
            this.xForward = 0;
            this.yForward = 0;
            return;
        }


        if (this.xForward != 0) {
            this.node.x += this.xForward * dt * this.speed;

        }
        else if (this.yForward != 0) {
            this.node.y += this.yForward * dt * this.speed;
        }

        if (this.xForward > 0) { this.canLeft = true; }
        else if (this.xForward < 0) { this.canRight = true; }
        else if (this.yForward > 0) { this.canDown = true; }
        else if (this.yForward < 0) { this.canUp = true; }


    }
});
